#include <iostream>
#include <string.h>
#include <queue>
#include <type_traits>
using namespace std;

const int N = 1010;
string s[N];
int n,m;
int xs,ys,xt,yt;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool st[N][N];

struct node
{
    int x,y,pos;
};

int main() {
    cin >> n >> m;
    cin >> xs >> ys >> xt >> yt;
    xs--,ys--,xt--,yt--;
    for(int i = 0;i < n;i++)
    {
        cin >> s[i];
    }
    queue<node> q;
    q.push({xs,ys,0});
    st[xs][ys] = true;
    while(q.size())
    {
        auto e = q.front();
        q.pop();
        if(e.x == xt && e.y == yt)
        {
            cout << e.pos << endl;
            return 0;
        }
        for(int i = 0;i < 4;i++)
        {
            int xx = e.x + dir[i][0];
            int yy = e.y + dir[i][1];
            if(xx >= n || xx < 0 || yy >= m || yy < 0 || s[xx][yy] == '*' || st[xx][yy])
            {
                continue;
            }
            q.push({xx,yy,e.pos + 1});
            st[xx][yy] = true;
        }
    }
    cout << -1 << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")